package cn.com.bsoft.cloud.controller.company;

import cn.com.bsoft.cloud.entity.AppointFileMess;
import cn.com.bsoft.cloud.entity.ScheduleRecord;
import cn.com.bsoft.cloud.entity.cloud.*;
import cn.com.bsoft.cloud.entity.param.DrugListParam;
import cn.com.bsoft.cloud.service.CloudAppService;
import cn.com.bsoft.cloud.service.CloudCompanyService;
import cn.com.bsoft.cloud.utils.ResultCode;
import cn.com.bsoft.immunity.kotlin.JsonExtKt;
import cn.com.bsoft.immunity.utils.HttpUtilsKt;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kotlin.jvm.internal.Reflection;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/clouddrug/")
public class CloudAppController {

    public static Logger logger= LogManager.getLogger(CloudAppController.class);

    @Autowired
    private CloudAppService cloudAppService;


    /**
     * app通过预约id查询对应的处方列表及详情
     * @return
     */
    @ResponseBody
    @RequestMapping("queryDrugByYyid")
    public Map<String,Object> queryDrugByYyid(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
            if (StringUtils.isBlank(map.get("yyidstr"))) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "预约id不能为空");
            }
            List<AppointMessSec> appointMesslist= cloudAppService.queryDrugByYyid(map.get("yyidstr"));
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result", appointMesslist);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("医药公司调用平台接口获取待配药列表,参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }



    /**
     * 云诊室app中对对应的预约添加
     * @return
     */
    @ResponseBody
    @RequestMapping("addAppointSite")
    public Map<String,Object> addAppointSite(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
            if (StringUtils.isBlank(map.get("yyid"))) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "预约id不能为空");
            }
            if (StringUtils.isBlank(map.get("psdbm"))) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "配送点编码不能为空");
            }
            if (StringUtils.isBlank(map.get("psdmc"))) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "配送点名称不能为空");
            }
            Map<String,Object> m= cloudAppService.addAppointSite(map.get("yyid"),map.get("psdbm"),map.get("psdmc"));
            regMsg.put("code", m.get("code"));
            regMsg.put("msg", m.get("msg"));
            regMsg.put("result", m.get("result"));
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("医药公司调用平台接口获取待配药列表,参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }



    /**
     * 通过捷医name查询详情
     * @return
     */
    @ResponseBody
    @RequestMapping("findMessByLogname")
    public Map<String,Object> findMessByLogname(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
            String logName=map.get("logName");
            if (StringUtils.isBlank(logName)) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "logName不能为空");
            }
            List<DoctorInformation> docmesslist= cloudAppService.findMessByLogname(logName);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result",docmesslist );
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("根据捷医loginname查询详情,参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 查询信息
     * @return
     */
    @ResponseBody
    @RequestMapping("queryScheduleRecordEx")
    public Map<String,Object> queryScheduleRecordEx(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
           /* if (StringUtils.isBlank(map.get("begin"))) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "开始时间不能为空");
            }
            if (StringUtils.isBlank(map.get("end"))) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "结束时间不能为空");
            }
            if (StringUtils.isBlank(map.get("hospitalCode"))) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "医院编码不能为空");
            }
            if (StringUtils.isBlank(map.get("deptCode"))) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "科室编码不能为空");
            }*/
            List<ScheduleRecord> docmesslist= cloudAppService.queryScheduleRecordEx(map);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result",docmesslist );
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询详情,参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }



    /**
     * 添加预约信息与上传文件地址
     * @return
     */
    @ResponseBody
    @RequestMapping("addAppointFileMess")
    public Map<String,Object> addAppointFileMess(@RequestBody AppointFileMess param){
        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
            if (StringUtils.isBlank(param.getRecordId())) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "预约id不能为空");
            }
            if (StringUtils.isBlank(param.getUploudUrl())) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "文件地址不能为空");
            }

            Boolean bool= cloudAppService.addAppointFileMess(param);
            if(bool){
                regMsg.put("code", ResultCode.SUCCESS);
                regMsg.put("msg", "插入成功");
            }else{
                regMsg.put("code", ResultCode.ERROR);
                regMsg.put("msg", "插入失败");
            }
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("添加预约信息与上传文件地址，参数:"+param+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }





    /**
     * 后台查询接口
     * @return
     */
    @ResponseBody
    @RequestMapping("queryUrl")
    public Map<String,Object> queryUrl(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
            List<UrlMess> list= cloudAppService.queryUrl(map);
            regMsg.put("result",list);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询云诊室视频文件地址，参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 查询医院列表
     * @return
     */
    @ResponseBody
    @RequestMapping("queryHosList")
    public Map<String,Object> queryHosList(){
        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
            URL u = new URL(ResultCode.hospitalMess);
            Map map = new HashMap();
            List<HospitalMess> hoslist = new ArrayList<HospitalMess>();
            hoslist = HttpUtilsKt.postJson(u, map, JsonExtKt.getListJavaType(Reflection.getOrCreateKotlinClass(Object.class)));
            String json = JSON.toJSON(hoslist).toString();
            hoslist = JSONArray.parseArray(json, HospitalMess.class);
            regMsg.put("result",hoslist);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("调用平台接口查询医院列表，"+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 调用平台接口查询医生信息并保存
     * @return
     */
    @ResponseBody
    @RequestMapping("addDocmess")
    public Map<String,Object> addDocmess(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
            String hosCode= map.get("hosCode");
            String idCard=map.get("idCard");
            String workNum=map.get("workNum");
            String hosName=map.get("hosName");
            if (StringUtils.isBlank(hosCode)) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "医院编码不能为空");
            }
            if (StringUtils.isBlank(hosName)) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "医院名称不能为空");
            }
            if (StringUtils.isBlank(idCard)) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "身份证号不能为空");
            }
            if (StringUtils.isBlank(workNum)) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "工号不能为空");
            }

            Map<String,Object> m= cloudAppService.addDocmess(hosCode,hosName,idCard,workNum);
            regMsg=m;
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("添加云诊室医生相关信息，参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }




    /**
     * 查询医院对应的云诊室科室或医生
     * @return
     */
    @ResponseBody
    @RequestMapping("queryCloudDoctor")
    public Map<String,Object> queryCloudDoctor(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
            List<HashMap<String, List<CloudDoctor>>> list= cloudAppService.queryCloudDoctor(map);
            regMsg.put("result",list);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询医院对应的云诊室科室或医生，参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }




}
